package com.lovo.ssc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.sscsys.dto.SellKillGoodsDto;
import com.lovo.ssc.entity.Seckill;
import com.lovo.ssc.entity.Shelves;
import com.lovo.ssc.service.ISeckillService;
import com.lovo.ssc.service.IShelvesService;
import com.lovo.util.MQUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/seckill")
@Api(tags = "后台促销模块003刘芬")
public class SeckillController {

    @Autowired
    IShelvesService shelvesService;

    @Autowired
    ISeckillService seckillService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    private Float discount;//折扣率

//    查询所有已上架的商品展示到后台前端促销维护页面
    @GetMapping("findAll")
    @ApiOperation("展示所有状态为已上架的商品")
    public List<Shelves> findAll(){
        return shelvesService.list();
    }


    //选择促销，修改售价，将促销实体传入促销审核队列
    @GetMapping("sellKillGoods/{goodsId}/{info}/{discount}")
    @ApiOperation("选择商品传入折扣率和原售价选择促销审核")
    public void sellKillGoods(@PathVariable("goodsId") String goodsId,
                              @PathVariable("info") String info,
                              @PathVariable("discount") String discount){

        Integer id = Integer.parseInt(goodsId);
        Shelves shelves = shelvesService.getById(id);
        //修改销售价
//        shelves.setGoodspricce(shelves.getGoodspricce()*Float.parseFloat(discount));
        this.discount = Float.parseFloat(discount);
//        shelves.setGoodsstate("1");//0-已上架  1-促销中 2-预售商品
        shelvesService.saveOrUpdate(shelves);

        SellKillGoodsDto dto = new SellKillGoodsDto();//组件dto传送到远程mq
        dto.setCode(shelves.getGoodscode());
        dto.setGoodsName(shelves.getGoodsname());
        dto.setInfo(info);
        dto.setGoodsId(id);
        dto.setSellprice(shelves.getGoodspricce());
        dto.setGoodstype(shelves.getGoodstype());
        dto.setGoodsspecs(shelves.getGoodsspecs());
        rabbitTemplate.convertAndSend(MQUtil.directExchangeSellKill,MQUtil.sellKillKeyOne,dto);

        Seckill seckill = new Seckill();//保存到本都数据库
        seckill.setCode(shelves.getGoodscode());
        seckill.setGoodsName(shelves.getGoodsname());
        seckill.setInfo(info);
        seckill.setGoodsId(id);
        seckill.setSellprice(shelves.getGoodspricce());
        seckill.setDiscount(Float.parseFloat(discount));
        seckillService.saveOrUpdate(seckill);
    }

    //取消促销，恢复售价，将实体传入取消促销审核队列
    @GetMapping("cancelSellKill/{goodsId}/{info}")
    @ApiOperation("取消促销审核")
    public void cancelSellKill(@PathVariable("goodsId") String goodsId,
                               @PathVariable("info") String info) {
        int id = Integer.parseInt(goodsId);
        Shelves shelves = shelvesService.getById(id);
        if (this.discount == null){
            QueryWrapper qw = new QueryWrapper();
            qw.eq("code", shelves.getGoodscode());
            qw.eq("reason","申请促销");
            Seckill seckill = seckillService.getOne(qw);
            this.discount = seckill.getDiscount();
        }
        //修改销售价
//        shelves.setGoodspricce(shelves.getGoodspricce()/this.discount);
//        shelves.setGoodsstate("0");//0-已上架  1-促销中 2-预售商品

        shelvesService.saveOrUpdate(shelves);

        SellKillGoodsDto dto = new SellKillGoodsDto();//组件dto传送到远程mq
        dto.setCode(shelves.getGoodscode());
        dto.setGoodsName(shelves.getGoodsname());
        dto.setInfo(info);
        dto.setGoodsId(id);
        dto.setSellprice(shelves.getGoodspricce());
        dto.setGoodstype(shelves.getGoodstype());
        dto.setGoodsspecs(shelves.getGoodsspecs());
        rabbitTemplate.convertAndSend(MQUtil.directExchangeSellKill,MQUtil.sellKillKeyThree,dto);

        Seckill seckill = new Seckill();//保存到本地
        seckill.setCode(shelves.getGoodscode());
        seckill.setGoodsName(shelves.getGoodsname());
        seckill.setInfo(info);
        seckill.setGoodsId(id);
        seckill.setSellprice(shelves.getGoodspricce());
        seckill.setDiscount(this.discount);
        seckillService.saveOrUpdate(seckill);

    }

}
